﻿using GameFramework.DataTable;
using GameFramework;
using UnityEngine;
using System;
using System.Collections.Generic;

namespace MYSTARFORCE
{
    /// <summary>
    /// 战机抽象类
    /// </summary>
    [Serializable]
    public abstract class AircraftData : TargetableObjectData
    {

        // TODO: 推进器、武器、盔甲待完成
        //******************** WeaponDataList 武器列表 ********************//
        [SerializeField]
        private List<WeaponData> m_WeaponDatas = new List<WeaponData>();

        //******************** MaxHp 最大生命值 ********************//
        [SerializeField]
        private int m_MaxHp = 0;

        /// <summary>
        /// 最大生命值
        /// 由于父类的MaxHp属性是abstract的，因此在使用该属性的时候需要添加override关键字
        /// </summary>
        public override int MaxHp
        {
            get
            {
                return m_MaxHp;
            }
        }

        //******************** Defence 防御力 ********************//
        [SerializeField]
        private int m_Defence = 0;
        /// <summary>
        /// 防御力
        /// </summary>
        public int Defence
        {
            get
            {
                return m_Defence;
            }
        }

        //******************** DeadEffectID 死亡特效ID ********************//
        [SerializeField]
        private int m_DeadEffectId = 0;

        /// <summary>
        /// 死亡时的特效
        /// </summary>
        public int DeadEffectId
        {
            get
            {
                return m_DeadEffectId;
            }
        }

        //******************** DeadSoundId 死亡音效ID  ********************//
        [SerializeField]
        private int m_DeadSoundId = 0;

        /// <summary>
        /// 死亡时的音效
        /// </summary>
        public int DeadSoundId
        {
            get
            {
                return m_DeadSoundId;
            }
        }

        /// <summary>
        /// 移动速度
        /// </summary>
        public int Speed
        {
            get
            {
                //TODO: 待推进器制作完毕之后，返回推进器的speed属性
                return 5;
            }
        }

        /// <summary>
        /// AircraftData的构造方法
        /// </summary>
        /// <param name="entityId">实体id</param>
        /// <param name="typeId">实体类型id</param>
        /// <param name="camp">阵营类型id</param>
        public AircraftData(int entityId, int typeId, CampType camp) : base(entityId, typeId, camp)
        {

            //当我们实例化一个战机的时候，我们需要通过typeId查询对应的战机的武器、装甲、音效等数据
            IDataTable<DRAircraft> dtAircraft = GameEntry.DataTable.GetDataTable<DRAircraft>();

            DRAircraft drAircraft = dtAircraft.GetDataRow(TypeId);

            if (drAircraft == null)
            {
                return;
            }

            ////遍历weapon表， 添加该飞机的武器子实体数据
            for (int index = 0, weaponId = 0; (weaponId = drAircraft.GetWeaponIdAt(index)) > 0; index++)
            {
                AttachWeaponData(new WeaponData(GameEntry.Entity.GenerateSerialId(), weaponId, Id, Camp));
            }


            m_DeadEffectId = drAircraft.DeadEffectId;
            m_DeadSoundId = drAircraft.DeadSoundId;
        }

        /// <summary>
        /// 刷新战机的基础数据
        /// </summary>
        private void RefreshData()
        {
            //TODO: 待完成
        }

        public List<WeaponData> GetAllWeaponDatas()
        {
            return m_WeaponDatas;
        }

        /// <summary>
        /// 增加武器子实体的数据的方法
        /// </summary>
        /// <param name="weaponData"></param>
        public void AttachWeaponData(WeaponData weaponData)
        {
            if(weaponData == null)
            {
                return;
            }

            if (m_WeaponDatas.Contains(weaponData))
            {
                return;
            }

            m_WeaponDatas.Add(weaponData);
        }
    }
}